package com.duan.rabbitmq.work;

import com.duan.rabbitmq.utils.RabbitMqUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author db
 * @version 1.0
 * @description Consumer4
 * @since 2023/11/27
 */
public class Consumer4 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMqUtil.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();

        channel.basicQos(1);  // 每次消费一个消息
        // 消费者消费成功时的回调
        channel.queueDeclare("work",true,false,false,null);
        channel.basicConsume("work",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException{
                try{
                    Thread.sleep(2000);
                }catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println("消费者2： "+new String(body));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });


    }
}
